package com.platform.core.web.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Maps;
import com.platform.dto.CommonDataInfoDto;
import com.platform.comm.dto.DtoMapper;
import com.platform.comm.entity.CommDataInfoEntity;
import com.platform.comm.service.ICommDataInfoBiz;
import com.platform.vo.UploadApiDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author liujizheng
 * @version 1.0
 * @ClassName CommDataInfoController
 * @Description TODO
 * @date 2021/10/28 11:06
 */
@RestController
@RequestMapping("/management/commdata")
@Slf4j
public class CommDataInfoController {

    private final ICommDataInfoBiz dataInfoBiz;
    private final DtoMapper mapper;

    public CommDataInfoController(ICommDataInfoBiz dataInfoBiz, DtoMapper mapper) {
        this.dataInfoBiz = dataInfoBiz;
        this.mapper = mapper;
    }

    /**
     * 根据ID查询
     *
     * @param id id
     * @return 导入对象信息
     */

    @PostMapping("/queryCommDataImpById")
    public CommonDataInfoDto queryCommDataImpById(@RequestParam("id") String id) {
        return mapper.map(dataInfoBiz.queryCommDataImpById(id), CommonDataInfoDto.class);
    }

    @PostMapping("/downloadFile")
    public void downLoadFile(@RequestParam String id, HttpServletResponse response) throws IOException {
        IoUtil.copy(dataInfoBiz.downLoadFile(id), response.getOutputStream());
    }


    @PostMapping("/saveRecordRpc")
    public String saveRecordRpc(String originalFileName, BigDecimal size, InputStream uploadFile) {
        return ObjectUtil.defaultIfNull(dataInfoBiz.saveRecord(originalFileName, size, uploadFile), new CommDataInfoEntity()).getId();
    }

    /**
     *
     * @param originalFileName
     * @param filePath
     * @return
     */
    @PostMapping("/saveRecordRpcNew")
    public String saveRecordRpc(String originalFileName,String filePath) {
        File file = new File(filePath);
        try {
            if (file.exists()) {
                return ObjectUtil.defaultIfNull(dataInfoBiz.saveRecord(originalFileName, new BigDecimal(0),
                        new FileInputStream(file)), new CommDataInfoEntity()).getId();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return filePath;
    }

    @PostMapping("/saveUploadRecord")
    public CommonDataInfoDto saveUploadRecord(@RequestBody UploadApiDto uploadApiDto) {
        return mapper.map(this.dataInfoBiz.saveUploadRecord(uploadApiDto), CommonDataInfoDto.class);
    }

    @PostMapping(path = "/saveRecord", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonDataInfoDto saveRecord(@RequestPart(value = "file") MultipartFile multipartFile) {
        return mapper.map(this.dataInfoBiz.saveRecord(new HashMap<>(),multipartFile), CommonDataInfoDto.class);
    }

    /**
     * 文件上传
     */
    @PostMapping(name = "上传文件", path = "/uploadFiles", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public List<UploadApiDto> uploadFiles(@RequestPart(value = "files") MultipartFile[] attachments) {
        List<UploadApiDto> fileList = Lists.newArrayList();
        // 存储附件信息
        Arrays.stream(attachments).forEach(attach -> {
            Map<String, String> paramsMap = Maps.newHashMap();
            CommDataInfoEntity commDataInfoEntity = dataInfoBiz.saveRecord(paramsMap, attach);
            UploadApiDto uploadApiDto = new UploadApiDto();
            uploadApiDto.setId(commDataInfoEntity.getId());
            uploadApiDto.setFileName(commDataInfoEntity.getFileName());
            fileList.add(uploadApiDto);
        });
        return fileList;
    }
}
